package ar.edu.uba.fi.jmit.graphvisualizer;

import java.io.IOException;
import java.util.Properties;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Log4JInitServletListener implements ServletContextListener {

	@Override
	public void contextDestroyed(ServletContextEvent arg0) {
		
	}

	@Override
	public void contextInitialized(ServletContextEvent arg0) {
		Properties props = new Properties();
		try {
			if(this.getClass().getClassLoader().getResource("log4j-graphvisualizer.properties") == null) {
				Logger.getLogger(this.getClass()).warn("Log4J could not be configured.");
				return;
			}else {
				Logger.getLogger(this.getClass()).info("Configuring Log4j...");
			}
			props.load(this.getClass().getClassLoader().getResourceAsStream("log4j-graphvisualizer.properties"));
			Logger.getLogger(this.getClass()).info("Log4J configuration file loaded.");
			PropertyConfigurator.configure(props);
			Logger.getLogger(this.getClass()).info("Log4J was properly configured.");
		} catch (IOException exception) {
			System.out.println("Could not find Log4J configuration file");
			exception.printStackTrace();
		}
	}

}
